package com.bwie.contract.service.impl;

import com.bwie.common.domain.Contract;
import com.bwie.common.domain.request.ContractRequest;
import com.bwie.common.utils.TelSmsUtils;
import com.bwie.contract.mapper.ContractMapper;
import com.bwie.contract.service.ContractService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
public class ContractServiceImpl implements ContractService {

    @Resource
    private ContractMapper contractMapper;

    /**
     * 查询合同数量
     * @return
     */
    @Override
    public List<Contract> selectContractQuantity() {
        List<Contract> list = contractMapper.selectContractQuantity();
        return list;
    }


    /**
     * 查询合同
     * @param contract
     * @return
     */
    @Override
    public List<Contract> queryContract(ContractRequest contract) {
        List<Contract> contracts = contractMapper.queryContract(contract);
        return contracts;
    }

    @Override
    public Integer saveContract(Contract contract) {
        Integer i = contractMapper.saveContract(contract);
        return i;
    }


//    @Autowired
//    private RabbitTemplate rabbitTemplate;
//
//    private static final String SEND_SMS_KEY = "SEND_SMS_KEY";


    @Override
    public Integer updateContract(Contract contract) {
        Integer i = contractMapper.updateContract(contract);
        if (i>0){
            TelSmsUtils.sendSms(contract.getStoreManager(),new HashMap<String,String>(){{
                put("Pass the examination","你好,你上传的合同我们已经审核通过");
            }});
//            String uuid = UUID.randomUUID().toString();
//            rabbitTemplate.convertAndSend("SEND_SMS_KEY",uuid,"审核通过");
        }else {
            TelSmsUtils.sendSms(contract.getStoreManager(),new HashMap<String,String>(){{
                put("Pass the not examination","你好,你上传的合同我们已经审核未通过");
            }});
        }
        return i;
    }
}
